import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

const app = getApp();

Page({
    data: {
        member: 0,
        value: '',
        showAct: false,
        columns: [],
        act: '',
        historyAct: ''
    },
    onShareAppMessage() {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        });
    },
    async onReady() {
        const db = qq.cloud.database();
        const _ = db.command;
        let This = this;
        await db.collection("act")
            .where({
                'alliance': app.data.alliance
            })
            .get()
            .then(res => {
                this.setData({ act: res.data[0].act[res.data[0].act.length - 1] });
                this.setData({ columns: res.data[0].act });
                this.setData({ historyAct: res.data[0].act[0] });
            });
        await db.collection("sign-in").where({
                [this.data.act]: _.neq('√'),
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                This.setData({ member: res.data });
            });
        await db.collection("controlSign-in").where({
                [this.data.act]: _.neq('√'),
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                let member = This.data.member;
                res.data.map((value) => {
                    value.control = true;
                    member.push(value);
                })
                This.setData({ member: member });
            });
    },
    async retroactive(e) {
        let This = this;
        await Dialog.confirm({
                title: '提示',
                message: '请确认该成员是否参与此次战役',
            })
            .then(async() => {
                const db = qq.cloud.database();
                const _ = db.command;
                let admin = false
                await db.collection('administer').where({
                    unionId: _.eq(app.data.unionId)
                }).get().then((res) => {
                    if (res.data.length != 0) {
                        admin = true
                    }
                })
                if (admin) {
                    if (e.currentTarget.dataset.control) {
                        db.collection("controlSign-in")
                            .where({
                                userName: e.target.dataset.username,
                                unionId: e.target.dataset.id,
                                alliance: app.data.alliance
                            })
                            .update({
                                data: {
                                    [this.data.act]: _.set("√")
                                }
                            })
                            .then(res => {
                                Toast('补录成功');
                                db.collection("sign-in").where({
                                        [this.data.act]: _.neq('√'),
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        This.setData({ member: res.data });
                                    });
                                db.collection("controlSign-in").where({
                                        [this.data.act]: _.neq('√'),
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        let member = This.data.member;
                                        res.data.map((value) => {
                                            value.control = true;
                                            member.push(value);
                                        })
                                        This.setData({ member: member });
                                    });
                            }).catch(err => {
                                Toast('补录失败请重新补录');
                            })
                    } else {
                        db.collection("sign-in")
                            .where({
                                userName: e.target.dataset.username,
                                unionId: e.target.dataset.id,
                                alliance: app.data.alliance
                            })
                            .update({
                                data: {
                                    [this.data.act]: _.set("√")
                                }
                            })
                            .then(res => {
                                Toast('补录成功');
                                db.collection("sign-in").where({
                                        [this.data.act]: _.neq('√'),
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        This.setData({ member: res.data });
                                    });
                                db.collection("controlSign-in").where({
                                        [this.data.act]: _.neq('√'),
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        let member = This.data.member;
                                        res.data.map((value) => {
                                            value.control = true;
                                            member.push(value);
                                        })
                                        This.setData({ member: member });
                                    });
                            }).catch(err => {
                                Toast('补录失败请重新补录');
                            })
                    }
                } else {
                    Toast('没有管理员权限');
                }
            })
            .catch(() => {
                // on cancel
            });

    },
    onChange(event) {
        // event.detail 为当前输入的值
        console.log(event.detail);
    },
    switchAct() {
        this.setData({ showAct: true });
    },
    onClose() {
        this.setData({ showAct: false });
    },
    onChange(event) {
        const { picker, value, index } = event.detail;
        this.setData({ historyAct: value });
    },
    onClickLeft() {
        this.setData({ showAct: false });
    },
    async onClickRight() {
        const db = qq.cloud.database();
        const _ = db.command;
        let This = this;
        this.setData({ showAct: false });
        this.setData({ act: this.data.historyAct });
        await db.collection("sign-in").where({
                [this.data.act]: _.neq('√'),
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                This.setData({ member: res.data });
            });
        await db.collection("controlSign-in").where({
                [this.data.act]: _.neq('√'),
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                let member = This.data.member;
                res.data.map((value) => {
                    value.control = true;
                    member.push(value);
                })
                This.setData({ member: member });
            });
    },

})